﻿Public Class MainWindow
    Dim gen As New Generator
    Dim misc As New Misc()



    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataContext = misc
    End Sub

#Region "Event Handlers"

    Private Sub btnGenerate_Click(sender As Object, e As RoutedEventArgs)

        gen.getRandom(misc.Limit, misc.Letters, misc.Numbers, misc.Symbols, misc.Clipboard)
            txtOutput.Text = gen.Password.ToString
        gen.Password.Clear()
        If misc.Clipboard = True Then
        End If

    End Sub

    Private Sub btnExit_Click(sender As Object, e As RoutedEventArgs)
        Close()
    End Sub

    Private Sub menuAbout_Click(sender As Object, e As RoutedEventArgs)
        Dim about As New About()
        about.ShowDialog()
    End Sub

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)

        RecallSettings()
    End Sub

    Private Sub Window_Closing(sender As Object, e As ComponentModel.CancelEventArgs)

        SaveSettings()

    End Sub

    Private Sub check_Unchecked(sender As Object, e As RoutedEventArgs)
        CheckBoxValidation(checkLetters.IsChecked, checkNumbers.IsChecked, checkSymbols.IsChecked)
    End Sub

    Private Sub checkClip_Checked(sender As Object, e As RoutedEventArgs)
        MsgBox("Warning!  This feature should not be used on shared or public computers as it can " +
       "expose your password to other users.", MsgBoxStyle.Critical, "WARNING")
    End Sub

#End Region



    Private Sub SaveSettings()
        If misc.Letters = True And misc.Numbers = True And misc.Symbols = True And misc.Limit = 7 Then
            My.Settings.Reset()
        Else
            Dim message As String = "Would you like to save your settings for next time?"
            Dim msgBoxStyle As MsgBoxStyle = msgBoxStyle.YesNo
            Dim title As String = "Save"
            Dim response = MsgBox(message, msgBoxStyle, title)

            If response = MsgBoxResult.Yes And btnGenerate.IsEnabled = False Then
                misc.Save = False
            ElseIf response = MsgBoxResult.Yes Then
                misc.Save = True
            ElseIf response = MsgBoxResult.No Then
                misc.Save = False
                My.Settings.Reset()
            End If

            If misc.Save = True Then
                My.Settings.checkPrefs = misc.Save
                My.Settings.checkLetters = misc.Letters
                My.Settings.checkNumbers = misc.Numbers
                My.Settings.checkSymbols = misc.Symbols
                My.Settings.txtLimit = misc.Limit
            End If
            My.Settings.Save()

        End If


    End Sub


    Private Sub RecallSettings()
        'Load saved settings
        misc.Save = My.Settings.checkPrefs
        misc.Letters = My.Settings.checkLetters
        misc.Numbers = My.Settings.checkNumbers
        misc.Symbols = My.Settings.checkSymbols
        misc.Limit = My.Settings.txtLimit

        'Load the UI options.
        Me.checkLetters.IsChecked = misc.Letters
        Me.checkNumbers.IsChecked = misc.Numbers
        Me.checkSymbols.IsChecked = misc.Symbols
        Me.txtLimit.Text = misc.Limit
    End Sub

    Private Sub CheckBoxValidation(ByVal letters As Boolean, ByVal numbers As Boolean, ByVal symbols As Boolean)
        If letters = True Or numbers = True Or symbols = True Then
            Return
        Else
            checkLetters.IsChecked = True
        End If
    End Sub
End Class
